<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">    
<html xmlns="http://www.w3.org/1999/xhtml">   
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <%@ include file="/WEB-INF/pages/common/meta.jsp" %>
    <%@ include file="/WEB-INF/pages/common/common.jsp" %>
    <script type="text/javascript" src="${ctx}/static/js/main.js?time=New Date()"></script>
    <script type="text/javascript" src="${ctx}/static/js/workflow/workflowCommon.js?time=New Date()"></script>
    <script type="text/javascript" src="${ctx}/static/js/expand/easyui-validatebox.expandMethod.js"></script>
    <title>数据导出查看</title>
    <style type="text/css">
    	.td_center {
    		text-align:center;
    	}
    </style>
</head>

<body class="easyui-layout">
	<script type="text/javascript">
	//获取当前登录机构类型
	var cmsOrgType = decodeURI('${sessionScope.CURRENT_USER.cmsOrgType}');
	//获取当前登录机构编号
	var cmsBaseOrgCode = decodeURI('${sessionScope.CURRENT_USER.cmsBaseOrgCode}');
	var cmsOrgProperty;

	$(function() {
		var columns=[[{field : 'idStr',title : '',checkbox:true,width : 80},
	  			/*{field:'id',title:'文件编号',align: 'center'},*/
//	  			{field:'deal',title: '操作',align:'center',width: 80,formatter:lookExamineSkip},
	  			{field:'fileName',title:'文件名字',align: 'center',width :200,formatter: formatexportExcel,styler:styleSetCss},
	  			{field:'exportNum',title:'数据量',align: 'center',width : 80},
	  			{field:'fileUrl',align:'center',hidden:true},
	  			{field:'exportStatus',title:'任务状态',align: 'center',formatter: formatTaskStatus,width : 80},
	  			{field:'createTime',title:'任务开始时间',align: 'center',formatter: formatTime,width :150},
	  			{field:'productTime',title:'任务完成时间',align: 'center',formatter: formatTime,width :150},
	  			{field:'createOrg',title:'创建机构',align: 'center',width :150},
	  			{field:'createEmp',title:'创建人',align: 'center',width :150}
	  		   ]]; 
		newloadGrid("exportInfoType", columns,{
				url : "${pageContext.request.contextPath}/cs/exportFileController/findExportFileInfoByPage.do",
		    	pageSize:50,
	    	    toolbar:"#exportType",
	    	    singleSelect : 'false',
	    	    pagination : 'true',
				fitColumns : true
 			});
    		//开始时间，结束时间赋默认值
    		setDatetimebox();
	});
	function resetQuery(){
		setDatetimebox();
		$('#fileName').textbox('setValue','');
	}
	//格式化任务状态
    function formatTaskStatus(value,row){
        if(value == 0){
            return '待处理';
        }else if(value == 1){   
            return '处理中';
        }else if(value == 2){
            return '处理成功';
        }else if(value == 3){
            return '处理失败';
        }
    }
	//获取当前时间 格式 yyyy-MM-dd
	function getNowFormatDate() {
		//创建当前时间
	    var date = new Date();
	    var seperator1 = "-";
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
	            
	    return currentdate;
	} 
	//时间相差的天数 格式yyyy-MM-dd
	function daysBetween(DateOne,DateTwo)  
	{  
	    var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));  
	    var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);  
	    var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));  
	  
	    var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));  
	    var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);  
	    var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));  
	  
	    var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);   
	    return Math.abs(cha);  
	}  	
	/**
	 * 开始时间，结束时间 赋默认值
	 */
	function setDatetimebox(){
		var myDate = new Date();
		var year = myDate.getFullYear();
		var month = myDate.getMonth()+1;
		var date = myDate.getDate();
		var beginDate = year +"-"+ month +"-"+ date + " 00:00:00";
		var begin = Date.parse(beginDate);
		var endDate = year +"-"+ month +"-"+ date + " 23:59:59";
		var end = Date.parse(endDate);
		$("#createTimeBegin").datetimebox('setValue',formatTime(new Date(begin)));
		$("#createTimeEnd").datetimebox('setValue',formatTime(new Date(end)));
	}
    /**
	/** 导出 */
	function formatexportExcel(index,row){
        var fileName = row.fileName;
        var fileUrl = row.fileUrl;
		if(row.exportStatus == 2 && row.exportNum !=0){
		return "<a  href='${pageContext.request.contextPath}/cs/exportFileController/exportExcel.do?fileName=" + fileName+ "&fileUrl="+fileUrl+"'>" + fileName + "</a>";
		}else{
		return fileName;
		}
	}
	function styleSetCss(index,row){
	    if (row.exportStatus == 2 && row.exportNum !=0){
	        return 'background-color:#A2B700;';
	    } else {
	        return 'background-color:#FFBB1C;';
	    }
	} 
	//初始化查询的开始时间
	function initQueryBeginTime(){
		var date = new Date();
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var date = date.getDate();
		var beginTime = year + "-" + month + "-" + date + " 00:00:00";
		var begin = Date.parse(beginTime);
		return formatTime(new Date(begin));
	}
  	//初始化查询的结束时间
	function initQueryEndTime(){
		var date = new Date();
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var date = date.getDate();
		var endTime = year + "-" + month + "-" + date + " 23:59:59"
		var end = Date.parse(endTime);
		return formatTime(new Date(end));
	}
  	
	/**
	 * 扩展easyui表单验证
	 */
	$.extend($.fn.validatebox.defaults.rules,{
		//验证开始时间查询条件-不能大于结束时间
		checkTime:{
			validator: function(value){
				var beginTime = $('#createTimeBegin').datetimebox('getValue');
				var endTime = $('#createTimeEnd').datetimebox('getValue');
				if(beginTime > endTime){
					return false;
				}else{
					return true;
				}
			},
			message: "开始时间不能大于结束时间！"
		},
		//验证查询时间不能夸月
		checkTimeMonth:{
			validator: function(value){
				var beginTime = $('#createTimeBegin').datetimebox('getValue');
				var endTime = $('#createTimeEnd').datetimebox('getValue');
				var datecha = datedifference(beginTime,endTime);
				if(datecha > 30){
					return false;
				}else{
					return true;
				}
			},
			message: "查询时间不能相差31天！"
		},
	});
	
	function deleteFile(){
		var rows = $('#exportInfoType').datagrid('getSelections');
		if(rows.length > 0){
			var ids;
			for (var i = 0; i < rows.length; i++) {
			if(rows[i].exportStatus != 2){
				showInfoMsg("只能删除处理成功的文件!");
				return;
			}
			if(ids == null){
				ids = rows[i].id;
			}else{
				ids = rows[i].id+","+ids;
				}
	        }
			$.messager.confirm("确认", "您确定要删除吗？",
			function (r) {
                        if (r) {
                            $.ajax({
                                type: 'POST',
                                url: '${pageContext.request.contextPath}/cs/exportFileController/deleteFile.do',
                                data: {
                                    'ids': ids
                                },
                                success: function (data) {
                                    console.log("提交中");
                                    var result = data;
                                    if (result.success == true) {
                                        showInfoMsg("删除成功!");
                                        findExportFileInfo();
                                    } else {
                                        showInfoMsg("删除失败!");
                                    }
                                }
                            });
                        }
                    });
		}else{
			showInfoMsg("请选择一条数据!");
        	return;
		}
	}
	
	    //跳转查看
//  function lookExamineSkip(value,row,index){
//   	var fileName = row.fileName;
//      var fileUrl = row.fileUrl;
//		if(row.exportStatus == 2 && row.exportNum !=0){
//			ret='<a  class="iconfont uce-download" title="下载" href="javascript:void(0)" onclick="exportDownload(\''+index+'\')"></a>';
//			return ret;
//		}else{
//			return null;
//		}
//	}
//	    
//  function newDownload(fileName,fileUrl){
//  	if(fileName == null || fileUrl == null){
//  		showInfoMsg("请求有误!");
//      	return;
//  	}
//  	$.ajax({
//              type: 'get',
//              url: '${pageContext.request.contextPath}/cs/exportFileController/exportExcel.do?fileName=' + fileName+ '&fileUrl='+fileUrl,
//              success: function (data) {
//              }
//          });
//  }
    
    function exportDownload(index){
    	var row = $('#exportInfoType').datagrid('getRows')[index];
    	if(row.fileName == null || row.fileUrl == null){
    		showInfoMsg("请求有误!");
        	return;
    	}
    	out.clear(); 
		out = pageContext.pushBody();
    	$.ajax({
                type: 'get',
                url: '${pageContext.request.contextPath}/cs/exportFileController/exportExcel.do?fileName=' + row.fileName+ '&fileUrl='+row.fileUrl,
                success: function (data) {
                }
            });
    }
	/**
     * 查询导出Excel文件信息
     */
    function findExportFileInfo() {
        var beginTime = $('#createTimeBegin').datetimebox('getValue');
        var endTime = $('#createTimeEnd').datetimebox('getValue');
        var datecha = datedifference(beginTime,endTime);
        if(datecha > 31){
            showInfoMsg('查询时间不能相差32天！');
            return;
        }
        //点击按钮加载URL
        var opts = $("#exportInfoType").datagrid("options");
        opts.url = "../exportFileController/findExportFileInfoByPage.do";
        
        var isValid = $('#formFindexportInfoType').form('validate');
        if(isValid){
            $("#exportInfoType").datagrid("load", serializeFormObj("formFindexportInfoType"));
        }
    }
    /** 判断2个日期相差天数 */
    function datedifference(sDate1, sDate2) {
        var dateSpan,
            tempDate,
            iDays;
        sDate1 = Date.parse(sDate1);
        sDate2 = Date.parse(sDate2);
        dateSpan = sDate2 - sDate1;
        dateSpan = Math.abs(dateSpan);
        iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
        return iDays
    };
	/**
	 * 账单时间，结束时间 赋默认值
	 */
	function setFeetimebox(){
		var myDate = new Date();
		var year = myDate.getFullYear();
		var month = myDate.getMonth()+1;
		var date = myDate.getDate();
		var beginDate = year +"-"+ month +"-"+ date + " 00:00:00";
		var begin = Date.parse(beginDate);
		var endDate = year +"-"+ month +"-"+ date + " 23:59:59";
		var end = Date.parse(endDate);
		$("#feeTimeBegin").datetimebox('setValue',formatTime(new Date(begin)));
		$("#feeTimeEnd").datetimebox('setValue',formatTime(new Date(end)));
	}
</script>
    <div id="exportType" style="padding:5px;">
		<form action="#" id="formFindexportInfoType">
		<div style="margin:10px;">
			<table>
				<tbody>
					<tr>
						<td class="td_center">创建时间</td>
						<td><input name="createTimeBegin" id="createTimeBegin" class="easyui-datetimebox" data-options="editable:false,validType:['checkTime']"/>
						</td>
						<td class="td_center">至</td>
						<td><input name="createTimeEnd" id="createTimeEnd" class="easyui-datetimebox" data-options="editable:false"/></td>
						<td class="td_center">文件名字</td>
                        <td><input class="easyui-textbox" id="fileName" name="fileName"></td>
                        <td><a href="javascript:void(0)" class="easyui-linkbutton search" onclick="findExportFileInfo()" plain="true">查询</a></td>
                        <td><a href="javascript:void(0)" class="easyui-linkbutton reset" onclick="resetQuery()" plain="true"> 重置 </a></td>
					</tr>
                     </tr>
				</tbody>
				</table>
		 </div>
	    </form>
	    <div class="toolbar-margin">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="iconfont uce-delete" onclick="deleteFile()" plain="true">删除</a>
       </div>
	    <div style="text-align:right; border-top:1px solid gray;padding-right:35px;padding-top:10px;padding-bottom: 10px;">
	    		<span style="vertical-align:middle;margin-right:10px">当文件名为绿色时即可点击文件名下载</span>
                <span style="width: 20px;height: 20px;display:inline-block; background-color: #A2B700;border-radius:50%;vertical-align:middle"></span>
                <span style="vertical-align:middle">可下载</span>
                <span style="width: 20px; height: 20px;display:inline-block; background-color: #FFBB1C;border-radius:50%;vertical-align:middle"></span>
                <span style="vertical-align:middle;margin-right:10px">不可下载</span>
         </div>
	</div>
	
	<table id="exportInfoType" data-options="fit:true"></table>
</body>
</html>